package com.api.gbhg.lbj.api;

import com.alibaba.fastjson.JSON;
import com.weaver.general.BaseBean;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import weaver.conn.RecordSet;
import weaver.workflow.workflow.WorkflowVersion;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import static com.api.gbhg.lbj.tool.Tools.getsfkzsydwbxed;
import static com.api.gbhg.lbj.tool.Tools.getsubcompanyId;

/**
 * @author lbj
 * @version 1.0
 * @description: TODO 校验预算项目使用额度(主表)
 * @date 2021/5/17 9:46
 */
@Path("/gbhg/userMain")
public class UserQuotaMainCheckApi {
    private static final Logger logger = LoggerFactory.getLogger("gbhg");

    @POST
    @Path("/quotaMainCheck")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public String quotaMainCheck(Map<String, String> param) {
        logger.info("测试进入预算项目使用单位额度校验--quotaCheck");
        RecordSet rs1 = new RecordSet();
        Map<String, Object> map = new HashMap<>();
        BigDecimal money = new BigDecimal(param.get("money"));//申请报销金额
        String lcjlid = param.get("ysxm");//预算项目
        String bxr = param.get("bxr");//报销人
        String sydw = getsubcompanyId(bxr);
        RecordSet rs = getsfkzsydwbxed(lcjlid);
        rs.next();
        String sfkzsydwbxed = rs.getString("sfkzsydwbxed");
        String lcid = rs.getString("lcid");
        BaseBean baseBean=new BaseBean();
        String BZ18Workflowid = WorkflowVersion.getActiveVersionWFID(baseBean.getPropValue("FormationWorkflowid", "BZ18Workflowid"));//读取配置文件
        logger.info("是否控制使用单位报销额度:"+sfkzsydwbxed+",workflowid:"+lcid+",BZ18Workflowid:"+BZ18Workflowid);
        if (StringUtils.isNotBlank(sfkzsydwbxed)&&sfkzsydwbxed.equals("1")&&!lcid.equals(BZ18Workflowid)) {
            String sql = "select syed from uf_ysxmsydwfped where xmbzlc=" + lcjlid + " and sydw=" + sydw;
            rs1.executeQuery(sql);
            while (rs1.next()) {
                BigDecimal syed = new BigDecimal(rs1.getString("syed"));
                if (money.compareTo(syed) == 1) {
                    map.put("code", "300");
                    map.put("msg", "预算项目使用单位分配额度不足！");
                } else {
                    map.put("code", "200");
                }
            }
        } else {
            map.put("code", "200");
        }
        return JSON.toJSONString(map);
    }
}
